package com.fjec.market.helper;

import com.fjec.common.util.NumberUtil;
import com.fjec.market.entity.BizShopSeq;
import com.fjec.market.service.IBizShopSeqService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class MysqlShopNumHelper {

    @Resource
    private IBizShopSeqService shopSeqService;

    public Long genShopNum(int markCode) {
        // 查询markCode的序列是否存在
        BizShopSeq entity = shopSeqService.getById(markCode);
        boolean success;
        Long result;
        do {
            if (entity == null) {
                entity = new BizShopSeq();
                entity.setId(markCode);
                entity.setSeqValue(1L);
                result = 1L;
                success = shopSeqService.save(entity);
            } else {
                result = entity.getSeqValue() + 1;
                result = NumberUtil.giveUpNum(result, 4);
                entity.setSeqValue(result);
                success = shopSeqService.updateById(entity);
            }
        } while (!success);
        return result;
    }
}
